<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属客户" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入所属客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zxcrm:contact:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zxcrm:contact:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zxcrm:contact:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zxcrm:contact:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contactList" @selection-change="handleSelectionChange"  @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属客户" align="center" prop="customerId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="职务" align="center" prop="position" />
      <el-table-column label="关系等级" align="center" prop="relationshipLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zxcrm_relationship_level" :value="scope.row.relationshipLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="态度类别" align="center" prop="attitudeCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zxcrm_attitude_category" :value="scope.row.attitudeCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="直属上司id" align="center" prop="immediateSupervisorId" >
        <template slot-scope="scope">
          {{contactMap[scope.row.immediateSupervisorId]}}
        </template>
      </el-table-column>
      <el-table-column label="沟通风格" align="center" prop="communicationStyle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zxcrm_communication_style" :value="scope.row.communicationStyle"/>
        </template>
      </el-table-column>
      <el-table-column label="对对接人的要求" align="center" prop="contactPersonRequire" />
      <el-table-column label="对供应商的要求" align="center" prop="supplierRequire" />
      <el-table-column label="兴趣爱好" align="center" prop="hobby" />
      <el-table-column label="往期履历" align="center" prop="previousResume" />
      <el-table-column label="抽烟情况" align="center" prop="smokingSituation" />
      <el-table-column label="喝酒情况" align="center" prop="drinkingSituation" />
      <el-table-column label="婚姻状况" align="center" prop="maritalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zxcrm_marital_status" :value="scope.row.maritalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="家庭情况" align="center" prop="familySituation" />
      <el-table-column label="社会关系说明" align="center" prop="socialRelationshipsExplanation" />
      <el-table-column label="邮件地址" align="center" prop="mailingAddress" />
      <el-table-column label="家庭住址" align="center" prop="homeAddress" />
      <el-table-column label="其他备注" align="center" prop="otherRemarks" />
      <el-table-column label="决策角色" align="center" prop="decisionRole">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zxcrm_decision_role" :value="scope.row.decisionRole"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.stop="handleUpdate(scope.row)"
            v-hasPermi="['zxcrm:contact:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.stop="handleDelete(scope.row)"
            v-hasPermi="['zxcrm:contact:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改联系人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" label-position="top" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="所属客户" prop="customerId">
              <el-select v-model="form.customerId" placeholder="请选择所属客户" class="w100">
                <el-option
                  v-for="item in customerList"
                  :key="item.id"
                  :label="item.companyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div style="width: 100%;background-color: lightgray;height: 25px;align-items: center;">
              <div style="border-left: 10px;color: blue;"></div>
              <span style="text-align: center;">联系人信息</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别" class="w100">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="position">
              <el-input v-model="form.position" placeholder="请输入职务" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="决策角色" prop="decisionRole">
              <el-select v-model="decisionRoleList" multiple placeholder="请选择决策角色" class="w100" @change="handleDecisionRoleChange">
                <el-option
                  v-for="dict in dict.type.zxcrm_decision_role"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关系等级" prop="relationshipLevel">
              <el-select v-model="form.relationshipLevel" placeholder="请选择关系等级" class="w100">
                <el-option
                  v-for="dict in dict.type.zxcrm_relationship_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="籍贯" prop="nativePlace">
              <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日" prop="birthday" >
              <el-date-picker clearable  style="width: 100%;"
                v-model="form.birthday"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="态度类别" prop="attitudeCategory">
              <el-select v-model="form.attitudeCategory" placeholder="请选择态度类别" class="w100">
                <el-option
                  v-for="dict in dict.type.zxcrm_attitude_category"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="直属上司" prop="immediateSupervisorId">
              <el-select v-model="form.immediateSupervisorId" filterable placeholder="请选择沟通风格" class="w100">
                <el-option
                  v-for="item in contactListAll"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                 <span style="float: left">{{ item.name }}</span>
                 <span style="float: right; color: #8492a6; font-size: 13px">{{ item.customerCompanyName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="沟通风格" prop="communicationStyle">
              <el-select v-model="communicationStyleList" multiple placeholder="请选择沟通风格" class="w100" @change="handleCommunicationStyleList">
                <el-option
                  v-for="dict in dict.type.zxcrm_communication_style"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对对接人的要求" prop="contactPersonRequire">
              <el-input v-model="form.contactPersonRequire" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="对供应商的要求" prop="supplierRequire">
              <el-input v-model="form.supplierRequire" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兴趣爱好" prop="hobby">
              <el-input v-model="form.hobby" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="往期履历" prop="previousResume">
              <el-input v-model="form.previousResume" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽烟情况" prop="smokingSituation">
              <el-input v-model="form.smokingSituation" placeholder="请输入抽烟情况" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="喝酒情况" prop="drinkingSituation">
              <el-input v-model="form.drinkingSituation" placeholder="请输入喝酒情况" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚姻状况" prop="maritalStatus">
              <el-select v-model="form.maritalStatus" placeholder="请选择婚姻状况" class="w100">
                <el-option
                  v-for="dict in dict.type.zxcrm_marital_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="家庭情况" prop="familySituation">
              <el-input v-model="form.familySituation" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社会关系说明" prop="socialRelationshipsExplanation">
              <el-input v-model="form.socialRelationshipsExplanation" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="邮件地址" prop="mailingAddress">
              <el-input v-model="form.mailingAddress" placeholder="请输入邮件地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="家庭住址" prop="homeAddress">
              <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="12">
            <el-form-item label="其他备注" prop="otherRemarks">
              <el-input v-model="form.otherRemarks" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContact, listAllContact, getContact, delContact, addContact, updateContact } from "@/api/zxcrm/contact"
import { listAllCustomer } from "@/api/zxcrm/customer"

export default {
  name: "Contact",
  dicts: ['zxcrm_relationship_level', 'zxcrm_communication_style', 'zxcrm_attitude_category', 'zxcrm_decision_role', 'sys_user_sex', 'zxcrm_marital_status'],
  data() {
    return {
      
      communicationStyleList:[],
      decisionRoleList:[],
      customerList:[],
      gutter:20,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人表格数据
      contactList: [],
      contactListAll: [],
      contactMap: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        name: null,
        sex: null,
        phone: null,
        position: null,
        relationshipLevel: null,
        nativePlace: null,
        birthday: null,
        attitudeCategory: null,
        immediateSupervisorId: null,
        communicationStyle: null,
        contactPersonRequire: null,
        supplierRequire: null,
        hobby: null,
        previousResume: null,
        smokingSituation: null,
        drinkingSituation: null,
        maritalStatus: null,
        familySituation: null,
        socialRelationshipsExplanation: null,
        mailingAddress: null,
        homeAddress: null,
        otherRemarks: null,
        decisionRole: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: '所属客户是必填项', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名是必填项', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getContactListAll()
  },
  methods: {
    handleRowClick(row, column, event){
      this.$router.push({ path: "/zxcrm/contactInfo",query: {id: row.id} });
    },
    /** 沟通风格改变 */
    handleCommunicationStyleList(e){
      this.form.communicationStyle = e.join(",")
    },
    /** 决策角色改变 */
    handleDecisionRoleChange(e){
      this.form.decisionRole = e.join(",")
    },
    /** 客户列表 */
    getCustomerList(){
      listAllCustomer().then(res=>{
        this.customerList = res.data
      })
    },
    /** 查询联系人列表 */
    getList() {
      this.loading = true
      listContact(this.queryParams).then(response => {
        this.contactList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getContactListAll() {
      this.loading = true
      listAllContact(this.queryParams).then(response => {
        this.contactListAll = response.data
        this.contactMap = this.contactListAll.reduce((map,item)=>{
          map[item.id] = item.name
          return map
        },{})
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.customerList = []
      this.form = {
        id: null,
        customerId: null,
        name: null,
        sex: null,
        phone: null,
        position: null,
        relationshipLevel: null,
        nativePlace: null,
        birthday: null,
        attitudeCategory: null,
        immediateSupervisorId: null,
        communicationStyle: null,
        contactPersonRequire: null,
        supplierRequire: null,
        hobby: null,
        previousResume: null,
        smokingSituation: null,
        drinkingSituation: null,
        maritalStatus: null,
        familySituation: null,
        socialRelationshipsExplanation: null,
        mailingAddress: null,
        homeAddress: null,
        otherRemarks: null,
        decisionRole: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getCustomerList()
      this.open = true
      this.title = "添加联系人"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getCustomerList()
      const id = row.id || this.ids
      getContact(id).then(response => {
        this.form = response.data
        if(this.form.decisionRole){
          this.decisionRoleList = this.form.decisionRole.split(",")
        }
        this.open = true
        this.title = "修改联系人"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContact(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addContact(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除联系人编号为"' + ids + '"的数据项？').then(function() {
        return delContact(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zxcrm/contact/export', {
        ...this.queryParams
      }, `contact_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
